Z3
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 5331 of file z3py.py.

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5334 of file z3py.py.

5335  def __init__(self, m, ctx):
5336  assert ctx != None
5337  self.model = m
5338  self.ctx = ctx
5339  Z3_model_inc_ref(self.ctx.ref(), self.model)
def __init__
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.

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

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.

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

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

5344  def __repr__(self):
5345  return obj_to_string(self)
def __repr__
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.

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

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

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

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

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

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

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

5347  def sexpr(self):
5348  """Return a textual representation of the s-expression representing the model."""
5349  return Z3_model_to_string(self.ctx.ref(), self.model)
def sexpr
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.

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

Field Documentation

ctx

Definition at line 5337 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(), Optimize.add_soft(), 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(), Optimize.model(), Fixedpoint.param_descrs(), Optimize.param_descrs(), Tactic.param_descrs(), Fixedpoint.parse_file(), Fixedpoint.parse_string(), Fixedpoint.query(), Fixedpoint.set(), Optimize.set(), Tactic.solver(), Fixedpoint.statistics(), Optimize.statistics(), Solver.to_smt2(), and Fixedpoint.update_rule().

model

Definition at line 5336 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().