Z3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Public Member Functions | Data Fields
ModelRef Class Reference
+ Inheritance diagram for ModelRef:

Public Member Functions

def __init__
 
def __del__
 
def __repr__
 
def sexpr
 
def eval
 
def evaluate
 
def __len__
 
def get_interp
 
def num_sorts
 
def get_sort
 
def sorts
 
def get_universe
 
def __getitem__
 
def decls
 
- Public Member Functions inherited from Z3PPObject
def use_pp
 

Data Fields

 model
 
 ctx
 

Detailed Description

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

Definition at line 5271 of file z3py.py.

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5274 of file z3py.py.

5275  def __init__(self, m, ctx):
5276  assert ctx != None
5277  self.model = m
5278  self.ctx = ctx
5279  Z3_model_inc_ref(self.ctx.ref(), self.model)
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__
Definition: z3py.py:5274
def __del__ (   self)

Definition at line 5280 of file z3py.py.

5281  def __del__(self):
5282  Z3_model_dec_ref(self.ctx.ref(), self.model)
void Z3_API Z3_model_dec_ref(__in Z3_context c, __in Z3_model m)
Decrement the reference counter of the given model.
def __del__
Definition: z3py.py:5280

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

5467  def __getitem__(self, idx):
5468  """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.
5469 
5470  The elements can be retrieved using position or the actual declaration.
5471 
5472  >>> f = Function('f', IntSort(), IntSort())
5473  >>> x = Int('x')
5474  >>> s = Solver()
5475  >>> s.add(x > 0, x < 2, f(x) == 0)
5476  >>> s.check()
5477  sat
5478  >>> m = s.model()
5479  >>> len(m)
5480  2
5481  >>> m[0]
5482  x
5483  >>> m[1]
5484  f
5485  >>> m[x]
5486  1
5487  >>> m[f]
5488  [1 -> 0, else -> 0]
5489  >>> for d in m: print("%s -> %s" % (d, m[d]))
5490  x -> 1
5491  f -> [1 -> 0, else -> 0]
5492  """
5493  if isinstance(idx, int):
5494  if idx >= len(self):
5495  raise IndexError
5496  num_consts = Z3_model_get_num_consts(self.ctx.ref(), self.model)
5497  if (idx < num_consts):
5498  return FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, idx), self.ctx)
5499  else:
5500  return FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, idx - num_consts), self.ctx)
5501  if isinstance(idx, FuncDeclRef):
5502  return self.get_interp(idx)
5503  if is_const(idx):
5504  return self.get_interp(idx.decl())
5505  if isinstance(idx, SortRef):
5506  return self.get_universe(idx)
5507  if __debug__:
5508  _z3_assert(False, "Integer, Z3 declaration, or Z3 constant expected")
5509  return None
Function Declarations.
Definition: z3py.py:587
def get_universe
Definition: z3py.py:5446
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.
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.
def is_const
Definition: z3py.py:995
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 __getitem__
Definition: z3py.py:5466
def get_interp
Definition: z3py.py:5360
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 5345 of file z3py.py.

5346  def __len__(self):
5347  """Return the number of constant and function declarations in the model `self`.
5348 
5349  >>> f = Function('f', IntSort(), IntSort())
5350  >>> x = Int('x')
5351  >>> s = Solver()
5352  >>> s.add(x > 0, f(x) != x)
5353  >>> s.check()
5354  sat
5355  >>> m = s.model()
5356  >>> len(m)
5357  2
5358  """
5359  return int(Z3_model_get_num_consts(self.ctx.ref(), self.model)) + int(Z3_model_get_num_funcs(self.ctx.ref(), self.model))
def __len__
Definition: z3py.py:5345
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 5283 of file z3py.py.

5284  def __repr__(self):
5285  return obj_to_string(self)
def __repr__
Definition: z3py.py:5283
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 5510 of file z3py.py.

5511  def decls(self):
5512  """Return a list with all symbols that have an interpreation in the model `self`.
5513  >>> f = Function('f', IntSort(), IntSort())
5514  >>> x = Int('x')
5515  >>> s = Solver()
5516  >>> s.add(x > 0, x < 2, f(x) == 0)
5517  >>> s.check()
5518  sat
5519  >>> m = s.model()
5520  >>> m.decls()
5521  [x, f]
5522  """
5523  r = []
5524  for i in range(Z3_model_get_num_consts(self.ctx.ref(), self.model)):
5525  r.append(FuncDeclRef(Z3_model_get_const_decl(self.ctx.ref(), self.model, i), self.ctx))
5526  for i in range(Z3_model_get_num_funcs(self.ctx.ref(), self.model)):
5527  r.append(FuncDeclRef(Z3_model_get_func_decl(self.ctx.ref(), self.model, i), self.ctx))
5528  return r
Function Declarations.
Definition: z3py.py:587
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
Definition: z3py.py:5510
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 5290 of file z3py.py.

5291  def eval(self, t, model_completion=False):
5292  """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`.
5293 
5294  >>> x = Int('x')
5295  >>> s = Solver()
5296  >>> s.add(x > 0, x < 2)
5297  >>> s.check()
5298  sat
5299  >>> m = s.model()
5300  >>> m.eval(x + 1)
5301  2
5302  >>> m.eval(x == 1)
5303  True
5304  >>> y = Int('y')
5305  >>> m.eval(y + x)
5306  1 + y
5307  >>> m.eval(y)
5308  y
5309  >>> m.eval(y, model_completion=True)
5310  0
5311  >>> # Now, m contains an interpretation for y
5312  >>> m.eval(y + x)
5313  1
5314  """
5315  r = (Ast * 1)()
5316  if Z3_model_eval(self.ctx.ref(), self.model, t.as_ast(), model_completion, r):
5317  return _to_expr_ref(r[0], self.ctx)
5318  raise Z3Exception("failed to evaluate expression in the model")
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:5290
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 5319 of file z3py.py.

5320  def evaluate(self, t, model_completion=False):
5321  """Alias for `eval`.
5322 
5323  >>> x = Int('x')
5324  >>> s = Solver()
5325  >>> s.add(x > 0, x < 2)
5326  >>> s.check()
5327  sat
5328  >>> m = s.model()
5329  >>> m.evaluate(x + 1)
5330  2
5331  >>> m.evaluate(x == 1)
5332  True
5333  >>> y = Int('y')
5334  >>> m.evaluate(y + x)
5335  1 + y
5336  >>> m.evaluate(y)
5337  y
5338  >>> m.evaluate(y, model_completion=True)
5339  0
5340  >>> # Now, m contains an interpretation for y
5341  >>> m.evaluate(y + x)
5342  1
5343  """
5344  return self.eval(t, model_completion)
def evaluate
Definition: z3py.py:5319
def eval
Definition: z3py.py:5290
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 5360 of file z3py.py.

Referenced by ModelRef.__getitem__().

5361  def get_interp(self, decl):
5362  """Return the interpretation for a given declaration or constant.
5363 
5364  >>> f = Function('f', IntSort(), IntSort())
5365  >>> x = Int('x')
5366  >>> s = Solver()
5367  >>> s.add(x > 0, x < 2, f(x) == 0)
5368  >>> s.check()
5369  sat
5370  >>> m = s.model()
5371  >>> m[x]
5372  1
5373  >>> m[f]
5374  [1 -> 0, else -> 0]
5375  """
5376  if __debug__:
5377  _z3_assert(isinstance(decl, FuncDeclRef) or is_const(decl), "Z3 declaration expected")
5378  if is_const(decl):
5379  decl = decl.decl()
5380  try:
5381  if decl.arity() == 0:
5382  r = _to_expr_ref(Z3_model_get_const_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5383  if is_as_array(r):
5384  return self.get_interp(get_as_array_func(r))
5385  else:
5386  return r
5387  else:
5388  return FuncInterp(Z3_model_get_func_interp(self.ctx.ref(), self.model, decl.ast), self.ctx)
5389  except Z3Exception:
5390  return None
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 get_as_array_func
Definition: z3py.py:5533
def is_const
Definition: z3py.py:995
def is_as_array
Definition: z3py.py:5529
def get_interp
Definition: z3py.py:5360
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 5406 of file z3py.py.

5407  def get_sort(self, idx):
5408  """Return the unintepreted sort at position `idx` < self.num_sorts().
5409 
5410  >>> A = DeclareSort('A')
5411  >>> B = DeclareSort('B')
5412  >>> a1, a2 = Consts('a1 a2', A)
5413  >>> b1, b2 = Consts('b1 b2', B)
5414  >>> s = Solver()
5415  >>> s.add(a1 != a2, b1 != b2)
5416  >>> s.check()
5417  sat
5418  >>> m = s.model()
5419  >>> m.num_sorts()
5420  2
5421  >>> m.get_sort(0)
5422  A
5423  >>> m.get_sort(1)
5424  B
5425  """
5426  if idx >= self.num_sorts():
5427  raise IndexError
5428  return _to_sort_ref(Z3_model_get_sort(self.ctx.ref(), self.model, idx), self.ctx)
def num_sorts
Definition: z3py.py:5391
def get_sort
Definition: z3py.py:5406
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 5446 of file z3py.py.

Referenced by ModelRef.__getitem__().

5447  def get_universe(self, s):
5448  """Return the intepretation for the uninterpreted sort `s` in the model `self`.
5449 
5450  >>> A = DeclareSort('A')
5451  >>> a, b = Consts('a b', A)
5452  >>> s = Solver()
5453  >>> s.add(a != b)
5454  >>> s.check()
5455  sat
5456  >>> m = s.model()
5457  >>> m.get_universe(A)
5458  [A!val!0, A!val!1]
5459  """
5460  if __debug__:
5461  _z3_assert(isinstance(s, SortRef), "Z3 sort expected")
5462  try:
5463  return AstVector(Z3_model_get_sort_universe(self.ctx.ref(), self.model, s.ast), self.ctx)
5464  except Z3Exception:
5465  return None
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
Definition: z3py.py:5446
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 5391 of file z3py.py.

Referenced by ModelRef.get_sort().

5392  def num_sorts(self):
5393  """Return the number of unintepreted sorts that contain an interpretation in the model `self`.
5394 
5395  >>> A = DeclareSort('A')
5396  >>> a, b = Consts('a b', A)
5397  >>> s = Solver()
5398  >>> s.add(a != b)
5399  >>> s.check()
5400  sat
5401  >>> m = s.model()
5402  >>> m.num_sorts()
5403  1
5404  """
5405  return int(Z3_model_get_num_sorts(self.ctx.ref(), self.model))
def num_sorts
Definition: z3py.py:5391
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 5286 of file z3py.py.

Referenced by Fixedpoint.__repr__().

5287  def sexpr(self):
5288  """Return a textual representation of the s-expression representing the model."""
5289  return Z3_model_to_string(self.ctx.ref(), self.model)
def sexpr
Definition: z3py.py:5286
Z3_string Z3_API Z3_model_to_string(__in Z3_context c, __in 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 5429 of file z3py.py.

5430  def sorts(self):
5431  """Return all uninterpreted sorts that have an interpretation in the model `self`.
5432 
5433  >>> A = DeclareSort('A')
5434  >>> B = DeclareSort('B')
5435  >>> a1, a2 = Consts('a1 a2', A)
5436  >>> b1, b2 = Consts('b1 b2', B)
5437  >>> s = Solver()
5438  >>> s.add(a1 != a2, b1 != b2)
5439  >>> s.check()
5440  sat
5441  >>> m = s.model()
5442  >>> m.sorts()
5443  [A, B]
5444  """
5445  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
def num_sorts
Definition: z3py.py:5391
def sorts
Definition: z3py.py:5429
def get_sort
Definition: z3py.py:5406

Field Documentation

ctx

Definition at line 5277 of file z3py.py.

Referenced by Probe.__eq__(), Probe.__ge__(), ModelRef.__getitem__(), ApplyResult.__getitem__(), Probe.__gt__(), Probe.__le__(), Probe.__lt__(), Probe.__ne__(), Fixedpoint.add_rule(), Tactic.apply(), ApplyResult.as_expr(), Fixedpoint.assert_exprs(), ApplyResult.convert_model(), ModelRef.decls(), ModelRef.eval(), Fixedpoint.get_answer(), Fixedpoint.get_assertions(), Fixedpoint.get_cover_delta(), ModelRef.get_interp(), Fixedpoint.get_rules(), ModelRef.get_sort(), ModelRef.get_universe(), Fixedpoint.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Fixedpoint.set(), Tactic.solver(), Fixedpoint.statistics(), Solver.to_smt2(), and Fixedpoint.update_rule().

model

Definition at line 5276 of file z3py.py.

Referenced by ModelRef.__del__(), ModelRef.__getitem__(), ModelRef.__len__(), ModelRef.decls(), ModelRef.eval(), ModelRef.get_interp(), ModelRef.get_sort(), ModelRef.get_universe(), ModelRef.num_sorts(), and ModelRef.sexpr().