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

Constructor & Destructor Documentation

def __init__ (   self,
  m,
  ctx 
)

Definition at line 5274 of file z3py.py.

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

Definition at line 5280 of file z3py.py.

5280  def __del__(self):
5281  Z3_model_dec_ref(self.ctx.ref(), self.model)
5282 
def __del__(self)
Definition: z3py.py:5280
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 5466 of file z3py.py.

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

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

5283  def __repr__(self):
5284  return obj_to_string(self)
5285 
def __repr__(self)
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.

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

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

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

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

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

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

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

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

5429  def sorts(self):
5430  """Return all uninterpreted sorts that have an interpretation in the model `self`.
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.sorts()
5442  [A, B]
5443  """
5444  return [ self.get_sort(i) for i in range(self.num_sorts()) ]
5445 
def sorts(self)
Definition: z3py.py:5429
def num_sorts(self)
Definition: z3py.py:5391
def get_sort(self, idx)
Definition: z3py.py:5406

Field Documentation

ctx
model